/*
 Copyright (C) 2010 Chris Hawkins. All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:
 
 * Redistributions of source code must retain the above copyright notice, this
 list of conditions and the following disclaimer.
 
 * Neither the name of the author nor the names of its contributors may be used
 to endorse or promote products derived from this software without specific
 prior written permission.
 
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#import "ConfigProvAWS.h"
#import "AsdEndpoint.h"
#import "AsdSignatureProvider.h"

@implementation TASDConfigProvEndpoint
- (id)              init {
    self = [super initWithKey: @"AwsEndpoint" 
                      default: [[ASDCfEndpoint getByCode: ASDEndpointUsEast] name] 
                        title: @"Endpoint" 
                      details: @"AWS region for SimpleDb data store"];
    if ( self != nil ) {
        self.choices = [ASDCfEndpoint getAll];
    }
    return self;
}
- (void)            populateDetailCell: (UITableViewCell*) cell_ key: (NSString*) valueKey_ {
    ASDCfEndpoint *endpoint = [[ASDCfEndpoint getByName: valueKey_] retain];
    [[cell_ textLabel]       setText: [endpoint name]];
    [[cell_ detailTextLabel] setText: [endpoint host]];
    [endpoint release];
}
@end

@implementation TASDConfigProvSignature
- (id)              init {
    self = [super initWithKey: @"AwsSignature" 
                      default: [ASDCfSignatureProvider invalidProviderName]
                        title: @"Signature" 
                      details: @"AWS Signature Provider"];
    if ( self != nil ) {
        self.choices = [ASDCfSignatureProvider getAllProvider];
    }
    return self;
}
- (void)            populateDetailCell: (UITableViewCell*) cell_ key: (NSString*) valueKey_ {
    ASDCfSignatureProvider *provider = [[ASDCfSignatureProvider getProvider: valueKey_] retain];
    [[cell_ textLabel]       setText: [provider uniquePublicName]];
    [[cell_ detailTextLabel] setText: [provider accessKeyID]];
    [provider release];
}
- (void)            setInitialValues {
    if ( 0 == [choices count] ) {
        ASDCfSignatureProvider *provider = [[ASDCfSignatureProvider invalidProvider] retain];
        [ASDCfSignatureProvider addProvider: provider];
        [provider release];
        provider = nil;
        
        NSString *path = [[[NSBundle mainBundle] pathForResource: @"AwsSignatures" ofType: @"plist"] retain];
        NSData   *data = [[NSData dataWithContentsOfFile: path] retain];
        if ( data != nil ) {
            NSString*               error       = nil;
            NSPropertyListFormat    plistFormat = 0;
            id                      plistId     = [NSPropertyListSerialization propertyListFromData: data 
                                                                                   mutabilityOption: NSPropertyListImmutable 
                                                                                             format: &plistFormat 
                                                                                   errorDescription: &error];
            if ( plistId != nil ) {
                NSObject* plistObj = plistId;
                [plistObj retain];
                if ( plistFormat == NSPropertyListXMLFormat_v1_0 || plistFormat == NSPropertyListBinaryFormat_v1_0 ) {
                    if ( [plistObj isKindOfClass: [NSDictionary class]] ) {
                        NSDictionary *dict   = [(NSDictionary*) plistObj retain];
                        NSEnumerator *keys   = [[dict keyEnumerator] retain];
                        NSString     *key;
                        while ( key = [keys nextObject] ) {
                            if ( [key hasPrefix: @"AwsId"] ) {
                                NSObject *value = [dict objectForKey: key];
                                if ( [value isKindOfClass: [NSDictionary class]] ) {
                                    NSDictionary *dictId     = (NSDictionary*) value;
                                    NSString     *name       = [dictId objectForKey: @"UniquePublicName"];
                                    NSString     *keyId      = [dictId objectForKey: @"AccessKeyID"];
                                    NSString     *secret     = [dictId objectForKey: @"SecretAccessKey"];
                                    if ( name != nil && keyId != nil && secret != nil ) {
                                        ASDCfSignatureProvider* provider = [[ASDCfSignatureProvider alloc]
                                                                            initWithName: name access: keyId secret: secret];
                                        [ASDCfSignatureProvider addProvider: provider];
                                        [provider release];
                                    }
                                }
                            }
                        }
                        [keys   release];
                        [dict   release];
                    }
                }
                [plistObj release];
            }
            [error     release];
        }
        [data    release];
        [path    release];
    }
}
@end
